/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package meuteste;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tiago
 */
public class TelaInicial extends javax.swing.JFrame {

    /**
     * Creates new form TelaInicial
     */
    private PortaSerial portaSerial;
    private TelaPingar1 telaPingar1;
    private TelaExibirRotas1 telaRotas;
    private TelaEnvioRTCBroadcast telaEnvioRTCBroadcast;
    private TelaRTCUnicast1 telaRTCUnicast1;
    private TelaReqRTC1 telaReqRTC1;
    private ThreadReqDados threadReqDados;
    private TelaFazendoRequisicao telaFazendoRequisicao;
    private TelaParouRequisicaoDados telaParouRequisicaoDados;
    private TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo;
    private TelaSemRotaGerente telaSemRotaGerente;
    private TelaGerenteSemResposta telaGerenteSemResposta;
    private TelaCoordenadorOcupado telaCoordenadorOcupado;
    private Conexao conexao;

    public TelaInicial(final PortaSerial portaSerial, Conexao conexao) throws Exception {
        initComponents();
        this.conexao = conexao;
        this.portaSerial = portaSerial;
        telaErroNoFormatoDoProtocolo = new TelaErroNoFormatoDoProtocolo();
        telaGerenteSemResposta = new TelaGerenteSemResposta();
        telaSemRotaGerente = new TelaSemRotaGerente();
        telaPingar1 = new TelaPingar1(portaSerial, telaGerenteSemResposta, telaSemRotaGerente, telaErroNoFormatoDoProtocolo, telaCoordenadorOcupado);
        telaRotas = new TelaExibirRotas1(portaSerial, telaSemRotaGerente, telaErroNoFormatoDoProtocolo, telaCoordenadorOcupado);
        telaEnvioRTCBroadcast = new TelaEnvioRTCBroadcast();
        telaRTCUnicast1 = new TelaRTCUnicast1(portaSerial, telaGerenteSemResposta, telaSemRotaGerente, telaErroNoFormatoDoProtocolo, telaCoordenadorOcupado);
        telaReqRTC1 = new TelaReqRTC1(portaSerial, telaGerenteSemResposta, telaSemRotaGerente, telaErroNoFormatoDoProtocolo, telaCoordenadorOcupado);
        threadReqDados = new ThreadReqDados(portaSerial);
        telaFazendoRequisicao = new TelaFazendoRequisicao();
        telaParouRequisicaoDados = new TelaParouRequisicaoDados();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botaoVisualizarRotas = new javax.swing.JButton();
        botaoPingar = new javax.swing.JButton();
        botaoRTCBroadcast = new javax.swing.JButton();
        botaoRTCUnicast = new javax.swing.JButton();
        botaoReqRTC = new javax.swing.JButton();
        botaoRequisitarDados = new javax.swing.JButton();
        jButtonGerarRotas = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("tela Inicial");

        botaoVisualizarRotas.setText("Visualizar Rotas");
        botaoVisualizarRotas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoVisualizarRotasMouseReleased(evt);
            }
        });

        botaoPingar.setText("Pingar");
        botaoPingar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoPingarMouseReleased(evt);
            }
        });

        botaoRTCBroadcast.setText("Ajuste Rtc Broadcast");
        botaoRTCBroadcast.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoRTCBroadcastMouseReleased(evt);
            }
        });

        botaoRTCUnicast.setText("Ajuste Rtc Unicast");
        botaoRTCUnicast.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoRTCUnicastMouseReleased(evt);
            }
        });

        botaoReqRTC.setText("Requisicao Rtc");
        botaoReqRTC.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoReqRTCMouseReleased(evt);
            }
        });

        botaoRequisitarDados.setText("Requisitar Dados");
        botaoRequisitarDados.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoRequisitarDadosMouseReleased(evt);
            }
        });

        jButtonGerarRotas.setText("Gerar Rotas");
        jButtonGerarRotas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonGerarRotasMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(botaoRequisitarDados, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botaoVisualizarRotas, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botaoPingar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botaoRTCBroadcast, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botaoRTCUnicast, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(botaoReqRTC, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButtonGerarRotas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(botaoVisualizarRotas)
                .addGap(18, 18, 18)
                .addComponent(botaoPingar)
                .addGap(18, 18, 18)
                .addComponent(botaoRTCBroadcast)
                .addGap(18, 18, 18)
                .addComponent(botaoRTCUnicast)
                .addGap(18, 18, 18)
                .addComponent(botaoReqRTC)
                .addGap(18, 18, 18)
                .addComponent(botaoRequisitarDados)
                .addGap(18, 18, 18)
                .addComponent(jButtonGerarRotas)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoPingarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoPingarMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            telaPingar1.setVisible(true);
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_botaoPingarMouseReleased

    private void botaoRTCBroadcastMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoRTCBroadcastMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            try {
                portaSerial.enviarRTCBroadcast();
                telaEnvioRTCBroadcast.setVisible(true);
            } catch (IOException | InterruptedException ex) {
                Logger.getLogger(TelaInicial.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_botaoRTCBroadcastMouseReleased

    private void botaoRTCUnicastMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoRTCUnicastMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            telaRTCUnicast1.setVisible(true);
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_botaoRTCUnicastMouseReleased

    private void botaoVisualizarRotasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoVisualizarRotasMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            telaRotas.setVisible(true);
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_botaoVisualizarRotasMouseReleased

    private void botaoReqRTCMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoReqRTCMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            telaReqRTC1.setVisible(true);
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_botaoReqRTCMouseReleased

    private void botaoRequisitarDadosMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoRequisitarDadosMouseReleased
        threadReqDados.setRequisitarDados();
        if (threadReqDados.getRequisitarDados()) {
            botaoRequisitarDados.setText("Parar requisicao");
            telaFazendoRequisicao.setVisible(true);
        } else {
            botaoRequisitarDados.setText("Fazer requisicao");
            telaParouRequisicaoDados.setVisible(true);
        }
    }//GEN-LAST:event_botaoRequisitarDadosMouseReleased

    private void jButtonGerarRotasMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonGerarRotasMouseReleased
        if (threadReqDados.getTerminouRequisicao() == true) {
            try {
                portaSerial.gerarRotas(); 
            } catch (IOException ex) {
                Logger.getLogger(TelaExibirRotas1.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InterruptedException exe) {
            }
        } else {
            telaFazendoRequisicao.setVisible(true);
        }
    }//GEN-LAST:event_jButtonGerarRotasMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaInicial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //         new TelaInicial(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoPingar;
    private javax.swing.JButton botaoRTCBroadcast;
    private javax.swing.JButton botaoRTCUnicast;
    private javax.swing.JButton botaoReqRTC;
    private javax.swing.JButton botaoRequisitarDados;
    private javax.swing.JButton botaoVisualizarRotas;
    private javax.swing.JButton jButtonGerarRotas;
    // End of variables declaration//GEN-END:variables
}
